package com.own.business.resource.business.service.impl;

import com.own.business.resource.business.base.BaseFileRecordPathHandler;
import com.own.business.resource.business.service.FileRecordService;
import com.own.business.resource.core.property.ResourceProperty;
import com.own.business.resource.entity.bo.FileRecordBo;
import com.own.business.resource.entity.map.FileRecordMapVo;
import com.own.business.resource.entity.po.FileRecord;
import com.own.business.resource.entity.query.FileRecordQuery;
import com.own.business.resource.entity.vo.FileRecordVo;
import com.own.business.resource.mapper.FileRecordMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.common.model.UpdateModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * FileRecordServiceImpl
 *
 * @author chenxueli
 * @date 2023-07-07 14:28:34
 */
@Slf4j
@Service
public class FileRecordServiceImpl extends AbstractBaseService<
        FileRecord,
        FileRecordBo,
        FileRecordVo,
        FileRecordMapVo,
        FileRecordQuery,
        FileRecordMapper
        > implements FileRecordService {

    private static final List<BaseFileRecordPathHandler> FILE_RECORD_PATH_HANDLER_LIST = new ArrayList<>();
    @Resource
    private FileRecordMapper fileRecordMapper;
    @Resource
    private List<BaseFileRecordPathHandler> fileRecordPathHandlerList;
    @Resource
    private ResourceProperty resourceProperty;

    @PostConstruct
    private void init() {
        fileRecordPathHandlerList.stream()
                .sorted(Comparator.comparing(BaseFileRecordPathHandler::sort))
                .forEach(baseFileRecordPathHandler -> {
                    FILE_RECORD_PATH_HANDLER_LIST.add(baseFileRecordPathHandler);
                    log.info("初始化文件路径处理器: {}", baseFileRecordPathHandler.getClass().getName());
                });
    }

    /**
     * 根据md5值查询文件信息
     *
     * @param md5 md5信息
     * @return 文件信息
     */
    @Override
    public List<FileRecordVo> getByMd5(String md5) {
        var wrapper = lambdaQueryWrapper()
                .eq(FileRecord::getMd5, md5)
                .orderByDesc(FileRecord::getGmtCreate);
        var poList = listByWrapper(wrapper);
        return poList.stream().map(FileRecordVo::new).toList();
    }

    /**
     * 根据md5值查询文件信息
     *
     * @param md5      md5信息
     * @param position 上传的位置信息
     * @return 文件信息
     */
    @Override
    public List<FileRecordVo> listByMd5(String md5, String position) {
        var wrapper = lambdaQueryWrapper()
                .eq(FileRecord::getMd5, md5)
                .eq(FileRecord::getPosition, position)
                .orderByDesc(FileRecord::getGmtCreate);
        return listByWrapper(wrapper).stream().map(FileRecordVo::new).toList();
    }

    /**
     * 根据md5值查询文件信息
     *
     * @param md5      md5信息
     * @param position 上传的位置信息
     * @return 文件信息
     */
    @Override
    public FileRecordVo getByMd5(String md5, String position) {
        var wrapper = lambdaQueryWrapper()
                .eq(FileRecord::getMd5, md5)
                .eq(FileRecord::getPosition, position)
                .orderByDesc(FileRecord::getGmtCreate);
        var po = getOneByWrapper(wrapper);
        return po == null ? null : new FileRecordVo(po);
    }

    /**
     * 根据md5值查询文件信息
     *
     * @param md5s md5信息
     * @return 文件信息
     */
    @Override
    public List<FileRecordVo> listByMd5(Collection<String> md5s) {
        var wrapper = lambdaQueryWrapper()
                .in(FileRecord::getMd5, md5s)
                .orderByDesc(FileRecord::getGmtCreate);
        return listByWrapper(wrapper).stream().map(FileRecordVo::new).toList();
    }

    /**
     * 根据md5值查询文件信息
     *
     * @param md5s     md5信息
     * @param position 上传的位置信息
     * @return 文件信息
     */
    @Override
    public List<FileRecordVo> listByMd5(Collection<String> md5s, String position) {
        return null;
    }

    /**
     * 添加信息
     *
     * @param fileRecord 添加对象
     * @return 添加标识
     */
    @Override
    public UpdateModel<FileRecordVo> add(FileRecord fileRecord) {
        // 判断当前的路径是否带有对应的要处理的参数
        if (
                resourceProperty.getSaveHandler().getEnable()
                        && !fileRecord.getPath().contains(resourceProperty.getSaveHandler().getParams())
        ) {
            FILE_RECORD_PATH_HANDLER_LIST.forEach(handler -> {
                handler.accept(resourceProperty.getSaveHandler().getParams(), fileRecord);
            });
        }
        return super.add(fileRecord);
    }

}
